<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 表格 border 添加边框 stripe -->
      <el-table border stripe :data="roleList">
        <el-table-column type="expand" label="展开" width="80">
          <template slot-scope="scope">

            <el-row class="red" v-for="t1  in scope.row.children" :key="t1.id">
              <!-- 左侧的列 -->
              <el-col :span="6"> <el-tag closable @close="delRolesRightId(scope.row, t1.id)"> {{ t1.authName }} </el-tag>
                <i class="el-icon-caret-right"> </i>
              </el-col>
              <!-- 右侧的列 -->
              <el-col :span="18">
                <el-row class="blue" v-for="t2  in t1.children" :key="t2.id">
                  <!-- 左侧的列 -->
                  <el-col :span="6"> <el-tag type="success" closable @close="delRolesRightId(scope.row, t2.id)">
                      {{ t2.authName }} </el-tag> </el-col>
                  <!-- 右侧咧 -->
                  <el-col :span="18">
                    <!-- 循环的是tag -->
                    <el-tag class="t3tag" v-for="t3 in t2.children" :key="t3.id" closable type="warning"
                      @close="delRolesRightId(scope.row, t3.id)">
                      {{ t3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" width="80">
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="280">
        </el-table-column>
        <!-- description -->
        <el-table-column prop="roleDesc" label="角色描述">
        </el-table-column>
        <el-table-column label="操作" width="380">
          <template slot-scope="scop">
            <div>
              <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
              <el-button type="danger" size="mini" icon="el-icon-delete" @click="delRoleById(scop.row.id)">删除</el-button>
              <el-button type="warning" icon="el-icon-setting" size="mini"
                @click="getRightTree(scop.row)">分配权限</el-button>
            </div>
          </template>

        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限 -->
    <el-dialog title="分配权限" :visible.sync="dialogTreeVisible" @close="checkedKeys = []">
      <!-- data显示属性数据 defaultProps 对象的映射关系  show-checkbox显示复选框
      node-key代表复选框是否选中 default-checked-keys选中的复选框   default-expand-all默认展开所有 -->
      <el-tree ref="roleTree" :data="treeData" :props="treeProps" show-checkbox default-expand-all node-key="id"
        :default-checked-keys="checkedKeys"></el-tree>,
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogTreeVisible = false">取 消</el-button>
        <el-button type="primary" @click="updataRight">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import request from '@/utils/request';
export default {
  data () {
    return {
      // 角色列表
      roleList: [],
      // 权限对话框
      dialogTreeVisible: false,
      // 对话框数据
      treeData: [],
      // 映射关系对象
      treeProps: {
        children: 'children',
        label: 'authName'
      },
      // 默认勾选的数组
      checkedKeys: [],
      // roleid
      roleid: ''
    }
  },
  mounted () {
    this.getRolesList()
  },
  // 是对象,方法的信息
  methods: {
    // 获取到角色列表
    async getRolesList () {
      // 配置对象
      const { data: { data, meta } } = await request({
        url: "/roles",
        method: "GET"
      })
      // 出现了问题
      if (meta.status !== 200) {
        this.$message.error("网络异常,稍后重试")
      }
      this.roleList = data
    },
    // 删除角色权限
    async delRolesRightId (role, rightid) {
      const isconfirm = await this.$confirm("确定要删除吗", "提示", {
        confirmButtonText: "确定",
        // 取消按钮文本
        cancelButtonText: "否",
        type: 'warning'
      }).catch(() => {
        // console.log(err);
        return this.$message.info("取消删除了")
      })
      // 确定的情况
      if (isconfirm !== "confirm") {
        return
      }
      // 发送数据到后台serve
      const { data: { data, meta } } = await request({
        url: `roles/${role.id}/rights/${rightid}`,
        method: "DELETE"
      })
      if (meta.status !== 200) {
        return this.$message.error("删除失败")
      }
      // 重新给role的权限赋值
      role.children = data
      this.$message.success("删除权限成功")
    },
    // 获取权限列表,tree的形式
    async getRightTree (role) {
      const { data: { data, meta } } = await request({
        url: '/rights/tree',
        method: 'GET'
      })
      if (meta.status !== 200) {
        return this.$message.error("网络异常,稍后重试")
      }
      this.treeData = data
      this.getCheckedKeys(role, this.checkedKeys)
      // console.log(this.checkedKeys);
      // console.log(data);
      this.dialogTreeVisible = true
      // 页面打开给roleid赋值
      this.roleid = role.id
    },
    // children全选子类,arr
    getCheckedKeys (node, arr) {
      // 有chid 说明不是第三城
      if (node.children) {
        node.children.forEach((item) => {
          // 递归,自己调自己
          this.getCheckedKeys(item, arr)
        });
      } else {
        arr.push(node.id)
      }
    },
    // 获取到当前tree里边 所有的勾选的节点
    async updataRight () {
      // console.log(this.$refs.roleTree);
      // console.log(this.$refs.roleTree.getCheckedKeys());
      // console.log(this.$refs.roleTree.getHalfCheckedKeys());
      // 获取到勾选的id,获取到半选的id
      const idarr = [...this.$refs.roleTree.getCheckedKeys(), ...this.$refs.roleTree.getHalfCheckedKeys()]
      // console.log(idarr.join(','));
      const { data: { meta } } = await request({
        url: `/roles/${this.roleid}/rights`,
        method: "POST",
        data: { rids: idarr.join(',') }
      })
      if (meta.status !== 200) {
        return this.$message.error("网络异常,稍后重试")
      }
      // 关闭和刷新
      this.dialogTreeVisible = false
      this.$message.success("分配权限成功")
      // 重新获取数据
      this.getRolesList()
    },
    // 通过id删除
    delRoleById (id) {

    }

  }
}
</script>

<style lang="less" scoped>
.el-row {
  border-top: 1px solid #333;
  margin-top: 5px;
}

.t3tag {
  margin-left: 10px;
}
</style>
